package com.wish.banking.controller;


import com.wish.banking.entity.*;
import com.wish.banking.service.ICfContractService;
import com.wish.banking.service.ICwReceiptPlanService;
import com.wish.banking.service.IJrProductService;
import com.wish.common.core.usconfig.common.MsgResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 收款计划表	！房态-杨子强	高露雯 前端控制器
 * </p>
 *
 * @author gaoluwen
 * @since 2022-04-04
 */
@RestController
@RequestMapping("/cw-receipt-plan")
public class CwReceiptPlanController {
    @Autowired
    ICwReceiptPlanService iCwReceiptPlanService;
    @Autowired
    IJrProductService iJrProductService;
    @Autowired
    ICfContractService iCfContractService;
    /**
     * 根据合同和保理产品计算该合同的保理申请明细
    */
    @PostMapping("factoringApply")
    public MsgResult factoringApply(@RequestBody FactorApp factorApp){
        //根据id找到合同数据
        CfContract cfContract = iCfContractService.getById(factorApp.getContractId());
        System.err.println(cfContract);
        //租约保理明细表 将合同数据copy到租约明细对象中
        JrFactoringDetail jrFactoringDetail = new JrFactoringDetail();
        BeanUtils.copyProperties(cfContract,jrFactoringDetail);
        jrFactoringDetail.setCfContractId(cfContract.getId());
        jrFactoringDetail.setRow(factorApp.getRow());

        //查询剩余租约期限 （未付款的房租账期）剩余租约金房租合计
        List<CwReceiptPlan> billList = iCwReceiptPlanService.selectRemainingLeaseBill(factorApp.getContractId());

        if(billList != null && billList.size() >0){
            //借款账期开始日
            jrFactoringDetail.setBorrowedStart(billList.get(0).getPaymentDaysBegin());
            //借款账期结束日
            jrFactoringDetail.setBorrowedEnd(billList.get(billList.size() - 1).getPaymentDaysEnd());
            // 应收租约金额：即剩余租约金房租合计
            BigDecimal shouldMonthRent = BigDecimal.ZERO;
            for (CwReceiptPlan cwReceiptPlan : billList) {
                shouldMonthRent = shouldMonthRent.add(cwReceiptPlan.getPaymentAmount());
            }
            jrFactoringDetail.setShouldMonthRent(shouldMonthRent);
            // 借款周期(多少个月 = 付几 * 剩余未付房租账期数)
            jrFactoringDetail.setFactoringPeriod(cfContract.getPaymentType()*billList.size());
            //根据id找产品
            JrProduct jrProduct = iJrProductService.getById(factorApp.getProductId());
            // 授信金额 = 应收租约金额*(授信额度/100) (备注：四舍五入)
            jrFactoringDetail.setFactoringCreditMoney(shouldMonthRent.multiply(new BigDecimal(jrProduct.getCreditLine()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)).setScale(2, BigDecimal.ROUND_HALF_UP));
            // 利息 = 保理授信总金额 *月利率 *月数  = 保理授信总金额 * 年利率/12 *月数 (备注：四舍五入)
            //jrFactoringDetail.setFactoringInterest(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(factorApp.getInterestRate()/(100 * 12))).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
            jrFactoringDetail.setFactoringInterest(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(factorApp.getInterestRate()/(100.0 * 12)).setScale(5, RoundingMode.HALF_UP)).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
            System.err.println("利息"+jrFactoringDetail.getFactoringInterest());
            // 服务费 = 保理授信金额*服务费率/12*月数(备注：四舍五入)
            //jrFactoringDetail.setServiceFee(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(factorApp.getServiceFee()/(100 * 12))).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
            jrFactoringDetail.setServiceFee(jrFactoringDetail.getFactoringCreditMoney().multiply(new BigDecimal(factorApp.getServiceFee()/(100.0 * 12)).setScale(5, RoundingMode.HALF_UP)).multiply(new BigDecimal(jrFactoringDetail.getFactoringPeriod())).setScale(2, BigDecimal.ROUND_HALF_UP));
            System.err.println("服务费"+jrFactoringDetail.getServiceFee());

            // 明细信息
            MsgResult msgResult = new MsgResult();
            System.err.println(jrFactoringDetail);
            msgResult.setMsgData(jrFactoringDetail);
            // 借款周期不能超过产品设定的分期期限
            if (null != jrProduct.getInstalmentPeriod() && -1L != jrProduct.getInstalmentPeriod() // 不是无限期
                    && jrFactoringDetail.getFactoringPeriod() > jrProduct.getInstalmentPeriod()) {
                // 租约期限超过保理产品期限
                msgResult.setMsgInfo("该合同剩余租期已经超过所选保理产品期限");
                msgResult.setCode(400);
                return msgResult;
            } else {
                // 当前合同可以申请保理
                return msgResult;
            }
        }else{
            //不可以申请保理
            return MsgResult.succeed(null);
        }
    }
}
